import { createRouter, createWebHistory } from 'vue-router'
import { useTitle } from '@vueuse/core'
import Layout from '@/layout/index.vue'
import Cookies from 'js-cookie'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Index',
      component: Layout,
      children: [
        {
          path: '/',
          name: 'home',
          component: () => import('../views/index/index.vue'),
          meta: {
            title: '首页',
          },
        },
        {
          path: '/industry',
          name: 'industry',
          component: () => import('../views/industry/index.vue'),
          meta: {
            title: '产业链',
          },
        },
        {
          path: '/talent',
          name: 'talent',
          component: () => import('../views/talent/index.vue'),
          meta: {
            title: '人才链首页',
          },
        },
        {
          path: '/funding',
          name: 'funding',
          component: () => import('../views/funding/index.vue'),
          meta: {
            title: '新研学首页',
          },
        },
        {
          path: '/job',
          name: 'job',
          component: () => import('../views/talent/job.vue'),
          meta: {
            title: '职位详情',
          },
        },
        {
          path: '/vita',
          name: 'vita',
          component: () => import('../views/talent/vita.vue'),
          meta: {
            title: '简历详情',
          },
        },
        {
          path: '/company',
          name: 'company',
          component: () => import('../views/company/index.vue'),
          meta: {
            title: '公司主页',
          },
        },
        {
          path: '/goods',
          name: 'goods',
          component: () => import('../views/goods/index.vue'),
          meta: {
            title: '商品详情',
          },
        },
        {
          path: '/order',
          name: 'order',
          component: () => import('../views/order/index.vue'),
          meta: {
            title: '订单详情',
            private: true,
          },
        },
        {
          path: '/module',
          name: 'module',
          component: () => import('../views/module/index.vue'),
          meta: {
            title: '开通模块',
          },
        },
        {
          path: '/ordersquare',
          name: 'ordersquare',
          redirect: '/ordersquare/index',
          children: [
            {
              path: '/ordersquare/index',
              name: 'ordersquareIndex',
              component: () => import('../views/orderSquare/index.vue'),
              meta: {
                title: '订单广场',
              },
            },
            {
              path: '/ordersquare/list',
              name: 'ordersquareList',
              component: () => import('../views/orderSquare/list.vue'),
              meta: {
                title: '订单列表',
              },
            },
            {
              path: '/ordersquare/detail',
              name: 'ordersquareDetail',
              component: () => import('../views/orderSquare/detail.vue'),
              meta: {
                title: '订单详情',
              },
            },
          ],
        },
        
        {
          path: '/bidding',
          name: 'bidding',
          redirect: '/bidding/index',
          children: [
            {
              path: '/bidding/index',
              name: 'biddingIndex',
              component: () => import('../views/bidding/index.vue'),
              meta: {
                title: '国际招投标',
              },
            },
            {
              path: '/bidding/list',
              name: 'biddingList',
              component: () => import('../views/bidding/list.vue'),
              meta: {
                title: '招投标列表',
                isSticky: true
              },
            },
            {
              path: '/bidding/detail',
              name: 'biddingDetail',
              component: () => import('../views/bidding/detail.vue'),
              meta: {
                title: '招投标详情',
              },
            },
          ],
        },
      ],
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/index.vue'),
      meta: {
        title: '登录',
      },
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/register/index.vue'),
      meta: {
        title: '注册',
      },
    },
  ],
})

const loginList = ['/login', '/register']

router.beforeEach((to, from, next) => {
  if (to.meta?.private) {
    if (Cookies.get('token')) {
      next()
    } else {
      next({
        name: 'login',
        query: {
          redirect: to.fullPath,
        },
      })
    }
  } else if (loginList.includes(to.path)) {
    if (Cookies.get('token')) {
      next('/')
    } else {
      next()
    }
  } else if (from?.query?.redirect) {
    next(from.query.redirect)
  } else {
    next()
  }
})

router.afterEach((to) => {
  if (to.name !== 'Redirect') {
    const title = to?.meta?.title
    const pageTitle = useTitle()
    pageTitle.value = title + ' | RCEP | RCEPMART | Supply chain | industry chain'
  }
})

export default router
